<!--
 * @Descripttion: 
 * @Author: liujing
 * @Date: 2021-07-23 15:23:43
 * @LastEditTime: 2021-09-24 18:02:56
-->
<template>
  <div class="breadcrumb">
    <el-breadcrumb class="app-breadcrumb" separator=">">
      <transition-group>
        <el-breadcrumb-item v-for="item in submenuItem" :key="item.path">
          <!-- <a @click.prevent="handleLink(item)">{{ item.meta.title }}</a> -->
          {{ item.meta.title }}
        </el-breadcrumb-item>
      </transition-group>
    </el-breadcrumb>
  </div>
</template>

<script>
export default {
  name: "",
  data() {
    return {
      submenuItem: []
    };
  },
  created() {
    this.getBreadcrumb();
  },
  Watch: {
    $route: function() {
      this.getBreadcrumb();
    }
  },
  methods: {
    getBreadcrumb() {
      this.submenuItem = [];
      let matched = this.$route.matched.filter(
        item => item.meta && item.meta.title
      ); //$route.matched 将会是一个包含从上到下的所有对象 (副本)。
      this.submenuItem = matched;
    },
    handleLink(item) {
      const { redirect, path } = item;
      if (redirect) {
        this.$router.push(redirect);
        return;
      }
      this.$router.push(this.pathCompile(path));
    },
    pathCompile(path) {
      const { params } = this.$route;
      var toPath = pathToRegexp.compile(path); //url 的正则表达式,快速填充 url 字符串的参数值。
      return toPath(params);
    }
  }
};
</script>

<style lang="scss" scoped>
.app-breadcrumb.el-breadcrumb {
  display: inline-block;
  font-size: 14px;
  line-height: 50px;
  margin-left: 10px;
  //   .no-redirect {
  //     color: #97a8be;
  //     cursor: text;
  //   }
}
</style>
